package com.luis.toolbox.inbound;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.atomic.AtomicLong;

@Component
public class CommitState {

    private final AtomicLong totalCommit = new AtomicLong(0);
    private long lastCommitTime;
    private final ConcurrentLinkedQueue<String> messageStore = new ConcurrentLinkedQueue<>();

    public CommitState commit() {
        this.lastCommitTime = System.currentTimeMillis();
        this.totalCommit.addAndGet(1);
        return this;
    }

    public long getLastCommitTime() {
        return lastCommitTime;
    }

    public ConcurrentLinkedQueue<String> getMessageStore() {
        return messageStore;
    }

    public CommitState addMessage(String message) {
        this.messageStore.add(message);
        return this;
    }

    @Override
    public String toString() {

        return "CommitState{" +
                " totalCommit=" + totalCommit.get() +
                " , lastCommitTime=" + DateFormatUtils.format(new Date(lastCommitTime),"yyyy-MM-dd HH:mm:ss.SSS") +
                " , messageToSend=" + messageStore.size() +
                " }";
    }
}
